package com.nomi.blog.controller;

import com.nomi.blog.entity.Article;
import com.nomi.blog.entity.DTO.ArticleSearchDTO;
import com.nomi.blog.service.ArticleService;
import com.nomi.blog.utils.MarkdownFileUploadUtils;
import com.nomi.blog.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/article")
public class ArticleController {
    private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);

    @Autowired
    private ArticleService articleService;

    @Autowired
    private MarkdownFileUploadUtils markdownFileUploadUtils;

    /**
     * 创建文章
     *
     * @param article
     * @param coverImage
     * @param markdownFile
     * @return
     */
    @PostMapping("/create")
    public Result createArticle(@RequestBody Article article,
                                @RequestParam(required = false) MultipartFile coverImage,
                                @RequestParam(required = false) MultipartFile markdownFile) throws IOException {
        if (coverImage != null && !coverImage.isEmpty()) {
            article.setCoverImage(coverImage.getBytes());
        }

        if (markdownFile != null && !markdownFile.isEmpty()) {
            logger.info("Received Markdown file: {}", markdownFile.getOriginalFilename());
            String filePath = markdownFileUploadUtils.uploadMarkdownFile(markdownFile);
            article.setContent(filePath);
        }

        return articleService.createArticle(article);
    }

    /**
     * 更新文章
     *
     * @param article
     * @param coverImage
     * @param markdownFile
     * @return
     * @throws IOException
     */
    @PutMapping("/update")
    public Result updateArticle(@RequestBody Article article,
                                @RequestParam(required = false) MultipartFile coverImage,
                                @RequestParam(required = false) MultipartFile markdownFile) throws IOException {
        if (coverImage != null && !coverImage.isEmpty()) {
            article.setCoverImage(coverImage.getBytes());
        }

        if (markdownFile != null && !markdownFile.isEmpty()) {
            String filePath = markdownFileUploadUtils.uploadMarkdownFile(markdownFile);
            article.setContent(filePath);
        }

        return articleService.updateArticle(article);
    }

    /**
     * 发布文章
     *
     * @param articleId
     * @return
     */
    @PostMapping("/publish/{articleId}")
    public Result publishArticle(@PathVariable Long articleId) {
        return articleService.publishArticle(articleId);
    }

    /**
     * 保存草稿
     *
     * @param article
     * @param coverImage
     * @param markdownFile
     * @return
     */
    @PostMapping("/saveDraft")
    public Result saveDraft(Article article,
                            @RequestParam(required = false) MultipartFile coverImage,
                            @RequestParam(required = false) MultipartFile markdownFile) throws IOException {
        if (coverImage != null && !coverImage.isEmpty()) {
            article.setCoverImage(coverImage.getBytes());
        }

        if (markdownFile != null && !markdownFile.isEmpty()) {
            logger.info("Received Markdown file: {}", markdownFile.getOriginalFilename());
            String filePath = markdownFileUploadUtils.uploadMarkdownFile(markdownFile);
            article.setContent(filePath);
        }

        return articleService.saveDraft(article);
    }


    /**
     * 文章搜索接口
     *
     * @param searchDTO
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/search")
    public Result searchArticles(ArticleSearchDTO searchDTO,
                                 @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return articleService.searchArticles(searchDTO, pageNum, pageSize);
    }

    /**
     * 文章搜索接口(不带分页)
     *
     * @param searchDTO
     * @return
     */
    @GetMapping("/searchWithoutPage")
    public Result searchArticlesWithoutPage(ArticleSearchDTO searchDTO) {
        return articleService.searchArticlesWithoutPage(searchDTO);
    }

    /**
     * 审核文章接口
     *
     * @param articleId
     * @param status
     * @return
     */
    @PostMapping("/review/{articleId}")
    public Result reviewArticle(@PathVariable Long articleId, @RequestParam String status) {
        return articleService.reviewArticle(articleId, status);
    }

    /**
     * 最新文章查看接口
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/latest")
    public Result getLatestArticles(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return articleService.getLatestArticles(pageNum, pageSize);
    }

    /**
     * 最新文章查看接口(不带分页)
     *
     * @return
     */
    @GetMapping("/latestWithoutPage")
    public Result getLatestArticlesWithoutPage() {
        return articleService.getLatestArticlesWithoutPage();
    }

    // 最热标签查看接口

    /**
     * 热门标签查看接口
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/hotTags")
    public Result getHotTags(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return articleService.getHotTags(pageNum, pageSize);
    }

    /**
     * 热门标签查看接口(不带分页)
     *
     * @return
     */
    @GetMapping("/hotTagsWithoutPage")
    public Result getHotTagsWithoutPage() {
        return articleService.getHotTagsWithoutPage();
    }

    // 最热文章查看接口

    /**
     * 最热文章查看接口
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/hotArticles")
    public Result getHotArticles(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return articleService.getHotArticles(pageNum, pageSize);
    }

    /**
     * 最热文章查看接口(不带分页)
     *
     * @return
     */
    @GetMapping("/hotArticlesWithoutPage")
    public Result getHotArticlesWithoutPage() {
        return articleService.getHotArticlesWithoutPage();
    }

    // 文章归档类型查看接口

    /**
     * 文章归档类型查看接口
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/archives")
    public Result getArticleArchives(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return articleService.getArticleArchives(pageNum, pageSize);
    }

    /**
     * 文章归档类型查看接口(不带分页)
     *
     * @return
     */
    @GetMapping("/archivesWithoutPage")
    public Result getArticleArchivesWithoutPage() {
        return articleService.getArticleArchivesWithoutPage();
    }

    // 根据标签查看文章接口

    /**
     * 根据标签查看文章接口
     *
     * @param tag
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/tag/{tag}")
    public Result getArticlesByTag(@PathVariable String tag,
                                   @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return articleService.getArticlesByTag(tag, pageNum, pageSize);
    }

    /**
     * 根据标签查看文章接口(不带分页)
     *
     * @param tag
     * @return
     */
    @GetMapping("/tagWithoutPage/{tag}")
    public Result getArticlesByTagWithoutPage(@PathVariable String tag) {
        return articleService.getArticlesByTagWithoutPage(tag);
    }

    // 根据归档类型查看文章接口

    /**
     * 根据归档类型查看文章接口
     *
     * @param archiveType
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/archiveType/{archiveType}")
    public Result getArticlesByArchiveType(@PathVariable String archiveType,
                                           @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return articleService.getArticlesByArchiveType(archiveType, pageNum, pageSize);
    }

    /**
     * 根据归档类型查看文章接口(不带分页)
     *
     * @param archiveType
     * @return
     */
    @GetMapping("/archiveTypeWithoutPage/{archiveType}")
    public Result getArticlesByArchiveTypeWithoutPage(@PathVariable String archiveType) {
        return articleService.getArticlesByArchiveTypeWithoutPage(archiveType);
    }

    /**
     * 获取所有标签
     *
     * @return
     */
    @GetMapping("/allTags")
    public Result getAllTags() {
        return articleService.getAllTags();
    }

    /**
     * 获取所有时间归档
     *
     * @return
     */
    @GetMapping("/timeArchives")
    public Result getTimeArchives() {
        return articleService.getTimeArchives();
    }

    /**
     * 查询用户发布的文章
     *
     * @param userId   用户ID
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @return 查询结果
     */
    @GetMapping("/user/{userId}")
    public Result getArticlesByUserId(@PathVariable Long userId,
                                      @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                      @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return articleService.getArticlesByUserId(userId, pageNum, pageSize);
    }

    /**
     * 查询文章详情
     *
     * @param articleId
     * @return
     */
    @GetMapping("/detail/{articleId}")
    public Result getArticleDetail(@PathVariable Long articleId) {
        return articleService.getArticleDetail(articleId);
    }

    /**
     * 增加文章阅读量
     *
     * @param articleId
     * @return
     */
    @PostMapping("/increaseViewCount/{articleId}")
    public Result increaseArticleViewCount(@PathVariable Long articleId) {
        return articleService.increaseArticleViewCount(articleId);
    }

    /**
     * 根据状态查询文章
     * @param userId
     * @param status
     * @return
     */
    @GetMapping("/list/{userId}")
    public Result getArticlesByStatus(
            @PathVariable Long userId,
            @RequestParam(required = false) String status
    ) {
        return articleService.getArticlesByStatus(userId, status);
    }
}
